//封装网络请求

const baseUrl = 'http://47.91.230.137:8086'
const request = (options = {}) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + options.url || '',
            method: options.type || 'GET',
            data: options.data || {},
            header: {
                "X-Litemall-Token": uni.getStorageSync('user')
            } || {},
        }).then((response) => {
            if (response.data.errno == 0) { //判断账号密码是否正确
                resolve(response); //正常的返回
            } else {
                uni.showToast({
                    icon: 'none',
                    title: response.data.errmsg
                })
            }
        }).catch(error => {
            let [err, res] = error;
            rejects(err) //异常返回
        })
    });
}
const get = (url, data, options = {}) => {
    options.type = 'get';
    options.data = data;
    options.url = url;
    return request(options)
}
const post = (url, data, options = {}) => {
    options.type = 'post';
    options.data = data;
    options.url = url;
    return request(options)
}
const put = (url, data, options = {}) => {
    options.type = 'put';
    options.data = data;
    options.url = url;
    return request(options)
}
const deletes = (url, data, options = {}) => {
    options.type = 'deletes';
    options.data = data;
    options.url = url;
    return request(options)
}
export default {
    request,
    get,
    post,
    put,
    baseUrl,
    deletes
}